# -*- coding: utf-8 -*-
from model import Collection, db
from time import time


class Classification(Collection):
    '''
* `_id` (ObjectId) - ID of classification
* `name` (string) - name of classification
* `subclass` ([dict]) - sub-classifications of this one
    * `name` - name of this subclass
    * `_id` - Id of this subclass
    * `icon` - url of icon
* `image` ([dict]) - list of images
    * `url` (string) - url of image
    * `authorId` (string) - ID of author
    * `tags` ([string]) - tags of image
* `superior` (ObjectId) - ID of last classification, '' if non-existent
* `type` (int) - 1 means normal node, 2 means special node, 3 means leave
* `icon` (string) - url of icon
* `updateTime` (long) - 更新时间
    '''

    collection = db.classification

    class Field(object):
        _id = '_id'
        name = 'name'
        subclass = 'subclass'
        image = 'image'
        superior = 'superior'
        type = 'type'
        icon = 'icon'
        updateTime = 'updateTime'

    class Subclass(object):
        name = 'name'
        _id = '_id'
        icon = 'icon'
        type = 'type'

    class Image(object):
        url = 'url'
        authorId = 'authorId'
        tags = 'tags'


    @staticmethod
    def new_classification(name, superior, type, icon):
        return Classification.insert({
            Classification.Field.name: name,
            Classification.Field.subclass: [],
            Classification.Field.image: [],
            Classification.Field.superior: superior,
            Classification.Field.type: type,
            Classification.Field.icon: icon,
            Classification.Field.updateTime: int(time() * 1000)
        })

    @staticmethod
    def add_subclass(class_id, subclass_id, subclass_name, icon, type):
        Classification.collection.update({
            Classification.Field._id: class_id
        }, {
            '$push': {
                Classification.Field.subclass: {
                    Classification.Subclass._id: subclass_id,
                    Classification.Subclass.name: subclass_name,
                    Classification.Subclass.icon: icon,
                    Classification.Subclass.type: type
                }
            },
            '$set': {
                Classification.Field.updateTime: int(time() * 1000)
            }
        })

    @staticmethod
    def add_image(class_id, url, author_id, tags):
        Classification.collection.update({
            Classification.Field._id: class_id
        }, {
            '$push': {
                Classification.Field.image: {
                    Classification.Image.url: url,
                    Classification.Image.authorId: author_id,
                    Classification.Image.tags: tags
                }
            },
            '$set': {
                Classification.Field.updateTime: int(time() * 1000)
            }
        })

    @staticmethod
    def remove_image(class_id, url):
        Classification.collection.update({ 
            Classification.Field._id: class_id
        }, { 
            '$pull': {  
                Classification.Field.image: { 
                    Classification.Image.url: url
                }
            },
            '$set': {
                Classification.Field.updateTime: int(time() * 1000)
            }
        })
